package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.approve.model.Apl_arrive;
import com.power.sifa.approve.model.pageInfo.Apl_arrivePageInfo;
import com.power.sifa.approve.api.IApl_arriveFeign;
import com.power.sifa.approve.service.Apl_arriveService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   送达信息控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_arriveController",tags={"送达信息"})
public class Apl_arriveController implements IApl_arriveFeign{

    @Resource
    private Apl_arriveService apl_arriveService;


    @Override
    @ApiOperation("查询送达信息分页列表")
    @SaCheckPermission("apl_arrive_list")
    public ResponseDTO list(Apl_arrivePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_arrive> page = this.apl_arriveService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_arrive apl_arrive : page) {
                apl_arrive.geneText();
                list.add(apl_arrive);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看送达信息")
    @SaCheckPermission("apl_arrive_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_arrive apl_arrive  = apl_arriveService.getById(id);
        if (apl_arrive != null) {
            apl_arrive.geneText();
        }
        return ResponseDTO.succData(apl_arrive);
    }

    @Override
    @ApiOperation("删除送达信息")
    @SaCheckPermission("apl_arrive_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_arriveService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改送达信息")
    @SaCheckPermission("apl_arrive_save")
    public ResponseDTO save(Apl_arrive apl_arrive) {
        Assert.notNull(apl_arrive, "apl_arrive不能为空");
        apl_arriveService.saveOrUpdate(apl_arrive);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除送达信息")
    @SaCheckPermission("apl_arrive_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_arriveService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_arrive_list")
    public void output(HttpServletResponse response, Apl_arrivePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "送达信息";
        String[] heads = {"applyid","受送达人","身份证号码","邮寄地址","收件人","移动电话","电子邮件","邮政编码","其他联系方式（qq、微信）"};
        String[] fields = {"id","name","cardno","addr","recipient","mobile","email","postcode","other"};
        Page<Apl_arrive> page = this.apl_arriveService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "送达信息";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid","受送达人","身份证号码","邮寄地址","收件人","移动电话","电子邮件","邮政编码","其他联系方式（qq、微信）"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","name","cardno","addr","recipient","mobile","email","postcode","other"};
        List<Apl_arrive> list = (List<Apl_arrive>)ExcelUtil.Import(fields, file, Apl_arrive.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_arriveService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO queryDeliveryPersonnel(String applyid) {
        try {
            return ResponseDTO.succData(this.apl_arriveService.queryDeliveryPersonnel(applyid));
        } catch (Exception e) {
            log.error("/apl_arrive/queryDeliveryPersonnel 查询可送达人员列表失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "查询可送达人员列表失败");
        }
    }

    /**
     * 查询送达信息-带出受送达人
     * @param applyid
     * @return
     */
    @Override
    public ResponseDTO queryArriveByApplyid(String applyid) {
        try {
            return ResponseDTO.succData(this.apl_arriveService.queryArriveByApplyid(applyid));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.wrap(500, "查询送达信息失败");
        }

    }

}
